package com.gsafety.wellsfort.service.knowledge;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gsafety.wellsfort.common.context.LoginContextUtil;
import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.dao.knowledge.CoursewareDAO;
import com.gsafety.wellsfort.dao.knowledge.CoursewarePrivilegeDAO;
import com.gsafety.wellsfort.dao.security.AdminDAO;
import com.gsafety.wellsfort.dao.security.MenuDAO;
import com.gsafety.wellsfort.dao.security.RoleDAO;
import com.gsafety.wellsfort.dao.security.UserLoginDAO;
import com.gsafety.wellsfort.domain.dto.courseware.CoursewareDTO;
import com.gsafety.wellsfort.domain.dto.courseware.CoursewareQueryDTO;
import com.gsafety.wellsfort.domain.model.knowledge.Courseware;
import com.gsafety.wellsfort.domain.model.knowledge.CoursewarePrivilege;
import com.gsafety.wellsfort.domain.model.security.Admin;
import com.gsafety.wellsfort.domain.model.security.Menu;
import com.gsafety.wellsfort.domain.model.security.Role;
import com.gsafety.wellsfort.domain.model.security.UserLogin;
import com.gsafety.wellsfort.domain.vo.CoursewarePrivilegeVO;
import com.gsafety.wellsfort.domain.vo.CoursewareVO;
import com.gsafety.wellsfort.domain.vo.TeachingMaterialVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CoursewareService {
    @Autowired(required = false)
    CoursewareDAO coursewareDAO;
    @Autowired(required = false)
    CoursewarePrivilegeDAO privilegeDAO;
    @Autowired(required = false)
    RoleDAO roleDAO;
    @Autowired(required = false)
    MenuDAO menuDAO;
    @Autowired(required = false)
    AdminDAO adminDAO;

    public ApiResponse<CoursewareDTO> saveCourseware(CoursewareDTO coursewareDto) {
        ApiResponse apiResponse = null;
        Courseware courseware = new Courseware();
        BeanUtils.copyProperties(coursewareDto, courseware);
        List<CoursewarePrivilege> privilegeList = coursewareDto.getCoursewarePrivilege();
        Integer coursewareId = coursewareDto.getId();
        Admin admin=LoginContextUtil.getAdmin();
        courseware.setModified(admin.getId());
        courseware.setUpdateTime(new Date());
        if (coursewareId != null) {
            privilegeDAO.deleteByCoursewareId(coursewareId);
            coursewareDAO.updateByPrimaryKeySelective(courseware);
        } else {
            courseware.setCreator(admin.getId());
            courseware.setCreateTime(new Date());
            coursewareDAO.insert(courseware);
            coursewareDto.setId(courseware.getId());
        }
        for (CoursewarePrivilege privilege : privilegeList) {
            privilege.setCoursewareId(courseware.getId());
            privilegeDAO.insert(privilege);
        }
        apiResponse = ApiResponse.success(coursewareDto);
        return apiResponse;
    }

    public ApiResponse deleteByCoursewareId(Integer id) {
        ApiResponse apiResponse = null;
        try {
            privilegeDAO.deleteByCoursewareId(id);
            coursewareDAO.deleteByPrimaryKey(id);
            apiResponse = ApiResponse.success("课件删除成功");
        } catch (Exception e) {
            apiResponse = ApiResponse.error();
            e.printStackTrace();
        }
        return apiResponse;
    }

    public ApiResponse<CoursewareVO> getCourseware(Integer id) {
        ApiResponse<CoursewareVO> apiResponse = null;
        try {

            List<CoursewarePrivilegeVO> privilegeVOList = new ArrayList<CoursewarePrivilegeVO>();
            List<CoursewarePrivilege> privilegeList = privilegeDAO.selectByCoursewareId(id);
            for (CoursewarePrivilege privilege : privilegeList) {
                CoursewarePrivilegeVO privilegeVO = new CoursewarePrivilegeVO();
                BeanUtils.copyProperties(privilege, privilegeVO);
                Role r = roleDAO.selectByPrimaryKey(privilege.getRoleId());
                Menu m = menuDAO.selectByPrimaryKey(privilege.getMenuId());
                if (m != null) privilegeVO.setMenuPrivilegeName(m.getName());
                if (r != null) privilegeVO.setRoleName(r.getName());
                privilegeVOList.add(privilegeVO);
            }
            Courseware courseware = coursewareDAO.selectByPrimaryKey(id);
            CoursewareVO courseVo = new CoursewareVO.VOBuilder(courseware).build();
            if (courseVo.getModified() != null) {
                Admin admin = adminDAO.selectByPrimaryKey(courseVo.getModified());
                if(admin!=null)courseVo.setModifiedName(admin.getName());
            }

            courseVo.setPrivilegeList(privilegeVOList);
            apiResponse = ApiResponse.success(courseVo);
        } catch (Exception e) {
            apiResponse = ApiResponse.error();
            e.printStackTrace();
        }
        return apiResponse;
    }

    public ApiResponse<List<CoursewareVO>> findByPageParams(CoursewareQueryDTO dtoParams) {
        ApiResponse<List<CoursewareVO>> apiResponse = null;
        Admin admin=LoginContextUtil.getAdmin();
        if(dtoParams.getResourceScope()!=null&&dtoParams.getResourceScope()!=0){
            if(admin!=null)dtoParams.setCreator(LoginContextUtil.getAdmin().getId());
        }
        Page page = PageHelper.startPage(dtoParams.getPageNo(), dtoParams.getPageSize());
        List<Courseware> resultList = coursewareDAO.selectByParam(dtoParams);
        List<CoursewareVO> voList = new ArrayList<>();

        CoursewareVO coursewarelVo;
        for (Courseware q : resultList) {
            ApiResponse<CoursewareVO> response=getCourseware(q.getId());
            if(response.getData()!=null){
                coursewarelVo=response.getData();
                voList.add(coursewarelVo);
            }
        }
        PageInfo pageInfo = new PageInfo(page);
        pageInfo.setList(resultList);
        apiResponse = ApiResponse.success(voList, (int) pageInfo.getTotal());
        return apiResponse;
    }
}
